#include <stdio.h>
#include <stdlib.h>
int binsearch(int x, int v[], int n);
int binsearch2(int x, int v[], int n);

int main() {
	
	printf("hello, world\n");
	int v[] = { 1, 2, 3, 4, 5 , 6, 7, 8};
	//int index = binsearch(5, v, 8);
	int index = binsearch2(5, v, 8);
	printf("Index: %d\n", index);
	return 0;
}

int binsearch2(int x, int v[], int n) {
	int low, high, mid;
	low = 0;
	high = n - 1;
	
	while (low < high) {
		mid = (low + high) / 2;
		if (x <= v[mid]) {
			high = mid;
		} else {
			low = mid + 1;
		}
	}
	if (x == v[low]) {
		return low;
	}
	return - 1;
}

int binsearch(int x, int v[], int n) {
	int low, high, mid;
	low = 0;
	high = n - 1;
	while (low <= high) {
		mid = (low + high) / 2;
		if (x < v[mid]) {
			high = mid - 1;
		} else if (x > v[mid]) {
			low = mid + 1;
		} else {
			return mid;
		}
	}
	return -1;
}